// test.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <fstream>
#include <iostream>
#include <json/JsonParser.hpp>
#include <Windows.h>
using namespace std;
using namespace json;

wstring read_text(){	
	std::ifstream ifs("e:\\projects\\cutt-reader\\trunk\\m8\\lib\\json\\test\\channels.txt");
	if(ifs.is_open()){			
		long l,m;
		l = ifs.tellg();
		ifs.seekg (0, ios::end);
		m = ifs.tellg();
		ifs.seekg(0,ios::beg);
		char *buffer = (char*)calloc(m,sizeof(char));
		while(!ifs.eof()){
			ifs.read(buffer,m);
		}		
		int size = MultiByteToWideChar( CP_UTF8, 0, buffer,-1, NULL,0 );
		wchar_t* wbuffer = (wchar_t*)calloc(size,sizeof(wchar_t));
		MultiByteToWideChar(CP_UTF8, 0, buffer, -1, (LPWSTR)wbuffer, size); 
		return wstring(wbuffer,size); 
	}
	return L"";
}


int _tmain(int argc, _TCHAR* argv[])
{
	wstring s = read_text();
	JsonParser parser;
	//wstring s = L"{\"a\":1}";
	JObject* root = (JObject*)parser.parse(s);
	//JInt ji(1);
	//fetch(&ji,0);
	//JString js(L"hello");
	//fetch(&js,0);
	//JArray ja;
	//ja.add(&ji);
	//ja.add(&js);
	//fetch(&ja,0);
	//JObject jo;
	//jo.set(L"int",&ji);
	//jo.set(L"string",&js);
	//jo.set(L"array",&ja);
	//fetch(&jo,0);
	//print_json_object(root);
	JArray* jChannels = (JArray*)(*root)[L"channels"];	
	for(int i = 0;i < 7;++ i){		
		if(i >= jChannels->size()){
			break;;
		}
		JObject* jChannel = (JObject*)((*jChannels)[i]);		
		
		String s = json_cast<String>((jChannel->items)[L"id"]);
		wcout << s << endl;

		
		
	}
	
	return 0;
}

